package cn.lidongyu.org.controller;

import cn.lidongyu.basic.AjaxResult;
import cn.lidongyu.basic.PageList;
import cn.lidongyu.org.domain.Department;
import cn.lidongyu.org.query.DepartmentQuery;
import cn.lidongyu.org.service.IDepartmentService;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    @GetMapping("/{id}")
    public Department QueryOne(@PathVariable("id") Long id){
        return departmentService.findOne(id);
    }
    @GetMapping
    public List<Department>QueryAll(){
        return departmentService.findAll();
    }
    @PutMapping
    public AjaxResult save(@RequestBody Department department) {

        if (department.getId() == null) {
            departmentService.add(department);
        } else {
            departmentService.update(department);
        }
        return AjaxResult.me();
    }
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){

            departmentService.delete(id);
            return AjaxResult.me();

    }
    @PostMapping
    public PageList<Department> findByQuery(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.findByQuery(departmentQuery);
    }
    @PatchMapping
    public AjaxResult deletemore(@RequestBody List<Long> ids){
        try {
            departmentService.deletemore(ids);
            return AjaxResult.me();
        }catch(Exception e) {
            return AjaxResult.me().setMessage("操作失败").setSuccess(false);
        }
    }
    @GetMapping("/tree")
    public List<Department>QueryTree(){
        return departmentService.QueryTree();
    }
}
